//
//  CTabBarItem.m
//  OperationPro
//
//  Created by yaxue wang on 16/9/27.
//  Copyright © 2016年 展鸿科技. All rights reserved.
//

#import "CTabBarItem.h"
@interface CTabBarItem()
@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, strong) UILabel *lblTitle;
@property (nonatomic, strong) UILabel *lblUnReadCount;
@end

@implementation CTabBarItem

/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */

+ (CTabBarItem *)initWithTabBarItem:(UITabBarItem *)tabBarItem{
    CTabBarItem *tabbaritem = [[CTabBarItem alloc] init];
    [tabbaritem setImage:tabBarItem.image];
    [tabbaritem setSelectImage:tabBarItem.selectedImage];
    [tabbaritem setTitle:tabBarItem.title];
    return tabbaritem;
}

+ (CTabBarItem *)initWithTitle:(NSString *)title image:(UIImage *)image selectedImage:(UIImage *)selectedimage controller:(id)controller{
    CTabBarItem *tabbaritem = [[CTabBarItem alloc] init];
    [tabbaritem setImage:image];
    [tabbaritem setSelectImage:selectedimage];
    [tabbaritem setTitle:title];
    [tabbaritem setController:controller];
    return tabbaritem;
}

- (instancetype)init{
    self = [super initWithFrame:CGRectMake(0, 0, WindowWidth / 5, 49)];
    if (self) {
        [self initView];
    }
    return self;
}

- (void) initView{
    _imageView = [[UIImageView alloc] init];
    [self addSubview:_imageView];
    
    _lblTitle = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, 22)];
    [_lblTitle setFont:F(9)];
    [_lblTitle setCenter:CGPointMake(self.frame.size.width / 2, self.frame.size.height * 2 / 3 + 5)];
    [_lblTitle setTextAlignment:NSTextAlignmentCenter];
    _lblTitle.textColor=C(@"727272");
    [self addSubview:_lblTitle];
    
    _lblUnReadCount = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 10, 10)];
    [self addSubview:_lblUnReadCount];
    [_lblUnReadCount setBackgroundColor:[UIColor redColor]];
    [_lblUnReadCount.layer setCornerRadius:_lblUnReadCount.frame.size.width / 2];
    [_lblUnReadCount setHidden:YES];
    [_lblUnReadCount.layer setMasksToBounds:YES];
}

- (void)setImage:(UIImage *)image{
    _image = image;
    [_imageView setImage:image];
    [_imageView setFrame:CGRectMake(0, 0, image.size.width, image.size.height)];
    [_imageView setCenter:CGPointMake(self.frame.size.width / 2, self.frame.size.height / 3 + 2)];
}

- (void)setTitle:(NSString *)title{
    [_lblTitle setText:title];
}

- (void)setSelected:(BOOL)selected{
    _selected = selected;
    if (selected) {
        [_imageView setImage:self.selectImage];
        _lblTitle.textColor=MainColor;

        [_imageView setFrame:CGRectMake(0, 0, self.selectImage.size.width, self.selectImage.size.height)];
        [_imageView setCenter:CGPointMake(self.frame.size.width / 2, self.frame.size.height / 3 + 2)];
    }else{
        [self setImage:_image];
        _lblTitle.textColor=C(@"727272");

    }
}

- (void)setUnReadCount:(NSInteger)unReadCount{
    if (unReadCount > 0) {
        [_lblUnReadCount setHidden:NO];
     
        [_lblUnReadCount setCenter:CGPointMake(_imageView.frame.origin.x + _imageView.frame.size.width - _lblUnReadCount.frame.size.width / 2, _imageView.frame.origin.y + _lblUnReadCount.frame.size.height / 2)];
    }else{
        [_lblUnReadCount setHidden:YES];
    }
}


@end
